package com.tiantianyuegou.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.tiantianyuegou.R;
import com.tiantianyuegou.model.MyNewsModel;
import com.tiantianyuegou.util.TimeUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/1/11.
 */
public class MyNewsAdapter extends BaseAdapter {

    private Context context;
    private String[] text_title;
    private String[] text_data;
    private String[] text_content;
    private LayoutInflater inflater;
    private List<MyNewsModel> myNewsModels;

    public MyNewsAdapter() {

    }

    public MyNewsAdapter(Context context, String[] text_title, String[] text_data, String[] text_content) {
        this.context = context;
        this.text_title = text_title;
        this.text_data = text_data;
        this.text_content = text_content;
        inflater = LayoutInflater.from(context);
    }

    public MyNewsAdapter(Context context) {
        this.context = context;
        myNewsModels = new ArrayList<>();
        inflater = LayoutInflater.from(context);
    }

    public void setData(List<MyNewsModel> myNewsModels) {
        this.myNewsModels = myNewsModels;
    }

    @Override
    public int getCount() {
        return myNewsModels.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Holder holder = null;
        if (view == null) {
            view = inflater.inflate(R.layout.item_list_news, null);
            holder = new Holder();
            holder.text_title = (TextView) view.findViewById(R.id.text_title);
            holder.text_data = (TextView) view.findViewById(R.id.text_data);
            holder.text_content = (TextView) view.findViewById(R.id.text_content);
            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }
        MyNewsModel myNews = myNewsModels.get(i);
        holder.text_title.setText(myNews.getTmTitle());
        holder.text_data.setText(TimeUtils.convertTimeYMDHMS(Long.valueOf(myNews.getTmAddtime())));
        holder.text_content.setText(myNews.getTmContent());

        return view;
    }

    public static class Holder {
        TextView text_title, text_data, text_content;
    }
}
